package __6完整示例

import (
	"errors"
)

type MusicManage struct {
	musics [] MusicEntry
}
func NewMusicManager() *MusicManage{
	return &MusicManage{make([]MusicEntry,0)}
}
func (m *MusicManage) Len() int {
	return len(m.musics)
}
func (m *MusicManage) Get(index int)(muisc *MusicEntry,err error){
	if index<0||index>=len(m.musics){
		return nil,errors.New("index out of range.")
	}
	return &m.musics[index],nil
}
func (m *MusicManage) Find (name string) *MusicEntry{
	if len(m.musics)==0{
		return nil
	}
	for _,m:=range m.musics{
		if m.Name ==name{
			return &m
		}
	}
	return nil
}
func (m *MusicManage)Add(music *MusicEntry){
	m.musics=append(m.musics,*music)
}
func (m *MusicManage)Remove(index int) *MusicEntry{
	if index <0||index >=len(m.musics){
		return nil
	}
	removeMusic:=&m.musics[index]
	//从数组切片中删除元素
	if index<len(m.musics)-1{
		m.musics=append(m.musics[:index-1],m.musics[index+1:]...)
	}else if index ==0{//删除仅有的一个元素
		m.musics=make([]MusicEntry,0)
	}else{
		m.musics=m.musics[:index-1]
	}
	return removeMusic
}